import mongoose from 'mongoose'
const Schema = mongoose.Schema

const ProductSchema = new Schema({
  title: String,
  price: String,
  intro: String,
  images: [
    String
  ],
  parameter: [
    {
      key: String,
      value: String
    }
  ],
  meta: {
    createAt: {
      type: Date,
      default: Date.now()
    },
    updateAt: {
      type: Date,
      default: Date.now()
    }
  }
})

ProductSchema.pre('save', function(next) {
  if (this.isNew) {
    this.meta.createAt = this.meta.updateAt = Date.now()
  } else {
    this.meta.updateAt = Date.now()
  }
  next()
})

mongoose.model('Product', ProductSchema)